package com.hs.srm.api.modules.othersysapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hs.srm.common.utils.R;
import com.sap.conn.jco.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author socik
 * @date 2019/11/28 9:57
 * @email socik@qq.com
 */
@RestController
@RequestMapping("/api")
@Api(tags = "信贷读取&信贷维护接口")
public class CreditlimitChangeNew {

    @ApiOperation(value = "信贷读取&信贷维护", notes = "传入IT_ITEM,MODE,IN_TAB")
    @GetMapping("/creditlimitChangeNew/{jsonArray}")
    public R getCrmData(@ApiParam(value = "传入IT_ITEM,MODE,IN_TAB一个JSON数组") @PathVariable("jsonArray") String jsonArray) {
        //获取所有的json数据转化map格式
        JSONObject jsonObject = JSON.parseObject(jsonArray);
        JSONObject ITITEM = JSON.parseObject(tirmFristAndLast(jsonObject.getString("IT_ITEM")));
        JSONObject MODE = JSON.parseObject(tirmFristAndLast(jsonObject.getString("MODE")));
        JSONObject INTAB = JSON.parseObject(tirmFristAndLast(jsonObject.getString("IN_TAB")));
        Map<String, Object> mapItItem = ITITEM;
        Map<String, Object> mapMode = MODE;
        Map<String, Object> mapINTAB = INTAB;
        JCoDestination destination = null;
        JCoFunction function = null;
        JCoParameterList list = null;
        JCoParameterList ImportList = null;
        JCoParameterList ExportList = null;
        try {
            destination = JCoDestinationManager.getDestination("IDOC_AFS");
            function = destination.getRepository().getFunction("ZFG_CRM_CREDITLIMIT_CHANGE_NEW");
            list = function.getTableParameterList();
            ImportList = function.getImportParameterList();
            ExportList = function.getExportParameterList();
        } catch (JCoException e) {
            e.printStackTrace();
        }
        mapMode.forEach(ImportList::setValue);
        //传入参数数据
        if("M".equals(mapMode.get("MODE"))){
            //传入结构化参数
            JCoStructure jCoStructure = ImportList.getStructure("IT_ITEM");
            mapItItem.forEach(jCoStructure::setValue);
        }else {
            //传入表结构
            //传入表数据
            JCoTable table = list.getTable("IN_TAB");
            table.appendRow();
            mapINTAB.forEach(table::setValue);
        }
        try {
            function.execute(destination);
        } catch (JCoException e) {
            e.printStackTrace();
        }
        //返回参数数据
        JCoTable returnTable = list.getTable("OUT_TAB");
        String error = ExportList.getString("G_ZMSG");
        XMLSerializer xmlSerializer = new XMLSerializer();
        String resutStr = xmlSerializer.read(returnTable.toXML()).toString();
        JSON json = null;
        if (returnTable.getNumRows() == 1) {
            json = JSONObject.parseObject(resutStr);
        } else if (returnTable.getNumRows() > 1) {
            json = JSONArray.parseArray(resutStr);
        } else {
            return R.msg(error);
        }
        return R.ok(json);
    }

    /**
     * @Description 去除首尾字符
     * @Param [text]
     * @Return void
     * @Author socik
     * @Date 2019/12/3 15:36
     */

    public static String tirmFristAndLast(String text) {
        return StringUtils.strip(StringUtils.strip(text, "["), "]");
    }
}
